package week_01;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * 给你一个字符串表达式 s ，请你实现一个基本计算器来计算并返回它的值。
 * <p>
 * 注意:不允许使用任何将字符串作为数学表达式计算的内置函数，比如 eval() 。
 * 输入：s = "(1+(4+5+2)-3)+(6+8)"
 * 输出：23
 * https://leetcode.cn/problems/basic-calculator/
 */
public class BasicCalculator_224 {

    public int calculate(String s) {
        char[] arr = s.toCharArray();

        List<String> tokens = new ArrayList<>();
        Stack<String> stack = new Stack<>();
        boolean isNum = false;
        int num = 0;
        // int
        for (char c : arr) {
            if (c <= '9' && c >= '0') {
                num = num * 10 + c - '0';
                isNum=true;
            }else if(isNum){
                isNum=false;
                //stack.push()

            }

        }
        return 0;
    }

    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack();
        for (int i = 0; i < tokens.length; i++) {
            String element = tokens[i];
            if (element.equals("*") || element.equals("/") || element.equals("-") || element.equals("+")) {
                int b = stack.pop();
                int a = stack.pop();
                stack.push(clac(a, b, element));
            } else {
                stack.push(Integer.valueOf(element));
            }
        }
        return stack.pop();
    }

    private Integer clac(int a, int b, String element) {
        switch (element) {
            case "+":
                return a + b;
            case "-":
                return a - b;
            case "*":
                return a * b;
            case "/":
                return a / b;
        }
        return -1;
    }
}
